home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 422_02 / dosutil / hem.c < prev    next >
C/C++ Source or Header  |  1994-03-20  |  3KB  |  146 lines

  1. /*
  2.  * This program displays any occurances of the first eight hardware
  3.  * interrupts on the IBM PC, which signal various processor exceptions.
  4.  *
  5.  * When HEM detects a hardware exception, It will open a small window
  6.  * in the center of the screen, displaying what type of exception
  7.  * has occured, and waits for you to press a key. After you press a key,
  8.  * HEM will close the text window, and attempt to terminate the
  9.  * application "gracefully" by exiting to DOS.
  10.  *
  11.  * To install HEM, you must execute the program, and specify HOT-KEYS
  12.  * on the command line as follows:
  13.  *        L - Left SHIFT
  14.  *        R - Right SHIFT
  15.  *        A - ALT
  16.  *        C - CONTROL
  17.  *        S - SysRq (Caution: some systems may not like this one)
  18.  *
  19.  *        eg: HEM LR    (Install with LEFT+RIGHT SHIFT for hotkeys)
  20.  *
  21.  * Any time you 'POP-INTO' HEM with the HOT-KEYS, it will reclaim the
  22.  * hardware interrupts. This allows you to bypass application programs
  23.  * which take them over.
  24.  *
  25.  * Copyright 1990-1994 Dave Dunfield
  26.  * All rights reserved.
  27.  *
  28.  * Permission granted for personal (non-commercial) use only.
  29.  *
  30.  * Compile command: cc hem -fop
  31.  */
  32. #include <stdio.h>
  33. #include <tsr.h>
  34.  
  35. extern int PSP;        /* COM files CODE segment */
  36. char *msg;
  37.  
  38. divide()    { showint("Divide by Zero"); }
  39. step()        { showint("Single-Step interrupt"); }
  40. nmi()        { showint("Non-Maskable interrupt"); }
  41. brkpt()        { showint("Breakpoint interrupt"); }
  42. overflow()    { showint("Overflow"); }
  43. prtsc()        { showint("Print-Screen/BOUND interrupt"); }
  44. opcode()    { showint("Illegal Opcode"); }
  45. nofpu()        { showint("Co-processor not present"); }
  46.  
  47. /*
  48.  * Display the interrupt that has occured
  49.  */
  50. showint(ptr)
  51.     char *ptr;
  52. {
  53.     asm {
  54.         MOV        DX,CS
  55.         MOV        DS,DX
  56.     }
  57.     msg = ptr;
  58.     asm {
  59.         MOV        SS,DX
  60.         MOV        SP,0
  61.     }
  62.     message();
  63.     exit(0);
  64. }
  65.  
  66. /*
  67.  * On POP-UP, reclaim interrupts & issue message
  68.  */
  69. popup()
  70. {
  71.     get_ints();
  72.     msg = "Interrupt vectors reclaimed";
  73.     message();
  74. }
  75.  
  76. /*
  77.  * Display a message in a window on the screen
  78.  */
  79. message()
  80. {
  81.     char *ptr;
  82.  
  83.     wopen(18, 10, 40, 3, 0xD070);
  84.     ptr = "HEM: \7";
  85.     while(*ptr)
  86.         wputc(*ptr++);
  87.     while(*msg)
  88.         wputc(*msg++);
  89.     wgetc();
  90.     wclose();
  91. }
  92.  
  93. /*
  94.  * Grab the hardware interrupt vectors
  95.  */
  96. get_ints()
  97. {
  98.     set_int(0, ÷);
  99.     set_int(1, &step);
  100.     set_int(2, &nmi);
  101.     set_int(3, &brkpt);
  102.     set_int(4, &overflow);
  103.     set_int(5, &prtsc);
  104.     set_int(6, &opcode);
  105.     set_int(7, &nofpu);
  106. }
  107.  
  108. /*
  109.  * Set an individual interrupt vector
  110.  */
  111. set_int(intr, addr)
  112.     int intr, addr;
  113. {
  114.     pokew(0, intr*4, addr);
  115.     pokew(0, intr*4+2, PSP);
  116. }
  117.  
  118. /*
  119.  * Main program, either TSR or display USE message
  120.  */
  121. main(argc, argv)
  122.     int argc;
  123.     int *argv[];
  124. {
  125.     int hot_keys;
  126.     char *ptr;
  127.  
  128.     fputs("Hardware Exception Monitor\n\nCopyright 1990-1994 Dave Dunfield\nAll rights reserved.", stderr);
  129. /* If RAM-resident, print startup message & TSR */
  130.     if(argc > 1) {
  131.         hot_keys = 0;
  132.         ptr = argv[1];
  133.         while(*ptr) switch(toupper(*ptr++)) {
  134.             case 'A' : hot_keys |= ALT;        break;
  135.             case 'C' : hot_keys |= CONTROL;    break;
  136.             case 'L' : hot_keys |= L_SHIFT;    break;
  137.             case 'R' : hot_keys |= R_SHIFT; break;
  138.             case 'S' : hot_keys |= SYS_REQ;    break;
  139.             default: abort("\n\nInvalid HOTKEY"); }
  140.         get_ints();
  141.         tsr(&popup, hot_keys, 1500); }
  142.  
  143. /* Not RAM-resident, display usage info */
  144.     fputs("\n\nUse: hem <hotkeys>", stderr);
  145. }
  146.